var random_code = (new Date()).getTime() + Math.random();
var api = "http://mytest.api.com/index.php/";

$(function () {
    $(".zc").hide();
    $(".zcdl1").click(function () {
        $(".zcdl1").css({"background-color": "aqua"});
        $(".zcdl2").css({"background-color": "#70cbde"});
        $(".zc").hide();
        $(".dl").show();

    });
    $(".zcdl2").click(function () {
        $(".zcdl2").css({"background-color": "aqua"});
        $(".zcdl1").css({"background-color": "#70cbde"});
        $(".dl").hide();
        $(".zc").show();

    });

    $("#codeimg_login").attr('src', api + 'common/captcha?type=login&random_code=' + random_code);
    $("#codeimg_regist").attr('src', api + 'common/captcha?type=regist&random_code=' + random_code);

});

$(".regist").click(function () {
    var username = $("#regisForm input[name='username']").val();
    if (!username) {
        alert('用户名不能为空！');
        return false;
    }
    var pass1 = $("#regisForm input[name='pwd']").val();
    var pass2 = $("#regisForm input[name='pwd_confirm']").val();
    if (!pass1) {
        alert('密码不能为空！');
        return false;
    }
    if (pass1 !== pass2) {
        alert('两次密码输入不一致！');
        return false;
    }
    var nk_name = $("#regisForm input[name='nk_name']").val();
    if (!nk_name) {
        alert('昵称不能为空！');
        return false;
    }
    var code = $("#regisForm input[name='code']").val();
    if (!code) {
        alert('验证码不能为空！');
        return false;
    }

    $("#regisForm input[name='random_code']").val(random_code);
    $.ajax({
        url: api + "login/regist",
        data: $('#regisForm').serialize(),
        type: "POST",
        dataType: "json",
        success: function (data) {
            console.log(data)
            if (data.status != 0) {
                alert(data.msg);
                return false;
            }
            alert('注册成功，请登录！');
            window.location.href = "login.html";
        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });

})

$(".login").click(function () {
    $("#loginForm input[name='random_code']").val(random_code);
    $.ajax({
        url: api + "login/login",
        data: $('#loginForm').serialize(),
        type: "POST",
        dataType: "json",
        success: function (data) {
            if (data.status != 0) {
                alert(data.msg);
                return false;
            }
            alert('登录成功！');
            localStorage.setItem('token', data.data.token)
            localStorage.setItem('avator', data.data.avator)
            localStorage.setItem('nickname', data.data.nickname)
            localStorage.setItem('can_send_wishes', data.data.can_send_wishes)
            window.location.href = "index.html";
        },
        error: function (e) {
            alert('访问出错！');
            return false;
        }
    });
})

$("#codeimg_regist").click(function () {
    $("#codeimg_regist").attr('src', api + 'common/captcha?random_code=' + random_code + '&type=regist&rand=' + Math.random());
})

$("#codeimg_login").click(function () {
    console.log('sss');
    $("#codeimg_login").attr('src', api + 'common/captcha?random_code=' + random_code + '&type=login&rand=' + Math.random());
})